function jmAjax(url,data,m,type,fun)
{
	$.ajax({
		type: m, //以post方式与后台沟通
		url : url, //与此asp页面沟通
		dataType:type,//从asp返回的值以 JSON方式
		data: data, //发给asp的数据有两项
		success: fun
	});
}

function searchNear()
{
	//$('#searchWin').popup('close');
	var searcNearValue = $("#searcNearID").val();
	var radioDistancesParam=document.getElementsByName("radioDistances");
	var radioDistanceValue;
	for(var i=0;i<radioDistancesParam.length;i++)
	{
		if(radioDistancesParam[i].checked)
		{
			radioDistanceValue=radioDistancesParam[i].value;  
			break;
		}
	}
	if(positionLng && positionLat)
	{
		var url = "json_wangdianInfoSearchPhone.asp";
		var data = "searcNearValue="+searcNearValue+"&radioDistanceValue="+radioDistanceValue+"&positionLng="+positionLng+"&positionLat="+positionLat;
		jmAjax(url,data,"get","text",searchNearResult);
	}
	document.body.style.overflow='hidden';
	window.history.back();
}

function searchNearResult(json)
{
	setTimeout(function(){
		$("#listResult").html("");
		map.clearOverlays();
		createPositionMarker();
		var obj = eval("("+json+")");
		//$("#listResult").append('<li data-role="divider" data-theme="a">查询结果</li>');
		if(obj.length>0)
		{
			var num = obj.length;
			var arrPoints = [new BMap.Point(positionLng,positionLat)];
			for (var i=0;i<num;i++)
			{
				(function(i){
					var oObj=obj[i];
					if ((/(^[1-9]\d*$)/.test(oObj.wangdianAddressLng)))
					{
						oObj.wangdianAddressLng = oObj.wangdianAddressLng/100000;
						oObj.wangdianAddressLat = oObj.wangdianAddressLat/100000;
					}
					else
					{
						oObj.wangdianAddressLng = oObj.wangdianAddressLng;
						oObj.wangdianAddressLat = oObj.wangdianAddressLat;
					}
					arrPoints.push(new BMap.Point(oObj.wangdianAddressLng,oObj.wangdianAddressLat));
					var marker = new BMap.Marker(new BMap.Point(oObj.wangdianAddressLng,oObj.wangdianAddressLat));
					map.addOverlay(marker);
					var html="<font style='font-weight:600;font-size:12px'>网点名称：</font><font style='font-size:12px'>"+oObj.wangdianName+"</font><br>";
					html+="<font style='font-weight:600;font-size:12px'>所在城市：</font><font style='font-size:12px'>"+oObj.province+"</font><br>";
					html+="<font style='font-weight:600;font-size:12px'>网点地址：</font><font style='font-size:12px'>"+oObj.wangdianAddress+"</font><br>";
					html+="<font style='font-weight:600;font-size:12px'>网点所属片区：</font><font style='font-size:12px'>"+oObj.wangdianAreaOwen+"</font><br>";
					html+="<font style='font-weight:600;font-size:12px'>网点所属客户：</font><font style='font-size:12px'>"+oObj.wangdianOwen+"</font><br>";
					html+="<font style='font-weight:600;font-size:12px'>备注信息：</font><font style='font-size:12px'>"+oObj.beizhu+"</font><br>";
					var infoWindow = new BMap.InfoWindow();
					infoWindow.setContent(html);
					infoWindow.setWidth(250);
					marker.addEventListener("click", function(){this.openInfoWindow(infoWindow);});
					$("#listResult").append('<li><a href="#nearResultName'+i+'" onclick="toCenterFun(\''+marker+'\',\''+escape(html)+'\','+obj[i].wangdianAddressLng+','+obj[i].wangdianAddressLat+')">'+obj[i].wangdianName+'</a></li>');
				})(i);
			}
		}
		else
		{
			//$("#listResult").css("height", "92");
			$("#listResult").append('<li><a href="#nearResultNameNull">无结果</a></li>');
			$("#nearSearchNull").popup("open");
		}
		$("#listResult").listview('refresh'); // This also works*/
		map.setViewport(arrPoints);
		//var params = map.getViewport(arrPoints);
		//setTimeout(function(){
			
			//map.panTo(params.center);
			//map.setZoom(params.zoom);
			//$("#getPositionMsgTrue").html(params.center+"||"+params.zoom);
		//},500);
	},500);
}

function toCenterFun(marker,infoWin,lng,lat)
{
	document.body.style.overflow='hidden';
	window.history.back();
	setTimeout(function(){map.panTo(new BMap.Point(lng,lat));},500);
}

function getPositionMsgFun()
{
	document.body.style.overflow='auto';
	//ui-btn-text
	//$("#btnCamera").html("选择照片");
	//$("#btnCamera").css({height:"45px",textAlign:"center",lineHeight:"45px"});
	//$("#divFile").empty();
	//$("#divFile").append("<input type='file' name='file' id='file' style='display:none'>");
	//$("#file").change(function(){
	//	$("#btnCamera").html($("#file").val());
	//})
	$("#getPositionLngValue").val(positionLng);
	$("#getPositionLatValue").val(positionLat);
	$("#getPositionLng").html("<font style='font-weight:600;font-size:14px'>经度</font><font style='font-size:12px'>"+positionLng+"</font>");
	$("#getPositionLat").html("<font style='font-weight:600;font-size:14px'>纬度</font><font style='font-size:12px'>"+positionLat+"</font>");
}

function uploadPositionMsg()
{
	if(positionLng && positionLat && $("#wangdianName").val() != "")
	{
		var url = "json_wangdianUploadInfoSave.asp";
		var data = "sType=add&lng="+positionLng+"&lat="+positionLat+"&wangdianName="+$("#wangdianName").val()+"&wangdianNumber="+$("#wangdianNumber").val()+"&landmarksName="+$("#landmarksName").val()+"&positionAddress="+$("#positionAddress").val();
		jmAjax(url,data,"get","text",uploadPositionMsgCallBack);
       // $("#getPositionMsgBar").popup("close");
	}
}

function uploadPositionMsgCallBack(obj)
{
	document.body.style.overflow='hidden';
	window.history.back();
	if(obj == "true")
	{
		setTimeout(function(){
			$("#getPositionMsgTrue").popup("open");
		},500);
	}
	else
	{
		setTimeout(function(){
			$("#getPositionMsgNull").popup("open");
		},500);
	}
}

function uploadPositionFun()
{
	//$("#uploadPosition").popup("close");
	if(positionLng && positionLat&&userTokenInfo)
	{
		var url = "json_uploadPositionInfoSave.asp";
		var data = "sType=add&lng="+positionLng+"&lat="+positionLat+"&userName="+userTokenInfo.email+"&userid="+userTokenInfo.userId+"&state="+userTokenInfo.state+"&uploadPositionTextarea="+document.getElementById("uploadPositionTextarea").value;
		jmAjax(url,data,"get","text",uploadPositionFunCallBack);
		
		document.body.style.overflow='hidden';
		window.history.back();
	}
	else
	{
		$("#uploadPositionResultMsg").html("<p><font style='font-weight:600'>上传失败</font></p>");
		$("#uploadPositionResultMsg").popup("open");
	}
}

function uploadPositionFunCallBack(obj)
{
	if(obj == "true")
	{
		$("#uploadPositionResultMsg").html("<p><font style='font-weight:600'>上传成功</font></p>");
		$("#uploadPositionResultMsg").popup("open");
	}
	else
	{
		$("#uploadPositionResultMsg").html("<p><font style='font-weight:600'>上传失败</font></p>");
		$("#uploadPositionResultMsg").popup("open");
	}
}

function getSelfPositionFun()
{
	if(positionLng && positionLat)
	{
		map.panTo(new BMap.Point(positionLng,positionLat));
	}
}

function getQueryString(name)
{
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}

function TPhoneBrowserInfo(){}
TPhoneBrowserInfo.isMSF = function()
{
	var flag=false;
	if((navigator.userAgent.match(new RegExp("iPhone","i")))||(navigator.userAgent.match(new RegExp("iPad","i"))))
	{
		flag=true;
	}
	return flag;
}
TPhoneBrowserInfo.isCL = function()
{
	var flag=false;
	if((navigator.userAgent.match(new RegExp("Android","i"))))
	{
		flag=true;
	}
	return flag;
}

/**
* 日期转化成时间戳
* 日期格式 2011-02-02 21:12:13
* time_str:日期段 2011-02-02
* fix_time:时间段 21:12:13
*/
function strtotime(time_str, fix_time)
{
	var time = (new Date()).getTime();
	if(time_str)
	{//有日期段
		var str = time_str.split('-');
		if (3 === str.length)
		{
			var year = parseInt(str[0]) - 0; 
			var month = parseInt(str[1]) - 0 - 1;//月份是从0开始的
			var day = parseInt(str[2]) - 0;
			if(fix_time)
			{//有时间段
				var fix = fix_time.split(':');
				if(3 === fix.length)
				{
					var hour = parseInt(fix[0]) - 0; 
					var minute = parseInt(fix[1]) - 0;
					var second = parseInt(fix[2]) - 0;
					time = (new Date(year, month, day, hour, minute, second)).getTime();
				}
			}
			else
			{
				time = (new Date(year, month, day)).getTime();
			}
		}
	}
	//getTime()获取的时间戳到了毫秒数
	//time = time / 1000;//转到到秒数
	return time;
}